package com.hyg.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by huangyugen on 2018/07/19
 * 消息路由
 * 上一章教程中我们建立了一个简单的日志记录系统，能够将消息广播到多个消费者。
 * 本章，我们将添加一个新功能，类似订阅消息的子集。例如：我们只接收日志文件中ERROR类型的日志。
 **/
public class RoutingSendDirect {

    private static final String EXCHANGE_NAME = "direct_logs" ;
    private static final String HOST = "192.168.1.91";
    //路由关键字
    private static final String[] routingKeys = new String[]{"info" ,"warning", "error"};

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(HOST);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
//        交换器的规则有：
//        direct （直连）
//        topic （主题）
//        headers （标题）
//        fanout （分发）
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");//交换器的名字，type
        //发送消息
        for(String severity:routingKeys){
            String message = "【"+severity+"】"+"Send the message level:" + severity;
            //String exchange, String routingKey, BasicProperties props, byte[] body
            channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes());
            System.out.println(" [x] Sent '" + severity + "':'" + message + "'");
        }

    }
}
